package com.cskaoyan.service.wx.homeindex;

import com.cskaoyan.bean.goods.MarketGoods;
import com.cskaoyan.bean.market.Brand;
import com.cskaoyan.bean.market.Category;
import com.cskaoyan.bean.promotion.Poster;
import com.cskaoyan.bean.promotion.coupon.Coupon;
import com.cskaoyan.bean.promotion.topic.Topic;
import com.cskaoyan.bean.wx_home_index.*;
import com.cskaoyan.mapper.*;
import com.cskaoyan.mapper.goodsMapper.MarketGoodsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * @Description:
 * @Author: WD created on 2021-09-18 17:46
 * @Version:
 */

@Service
public class HomeIndexServiceImpl implements HomeIndexService {

    /**
     * 微信小程序系统配置的字段
     */
    //新品的数
    private String wx_index_new = "market_wx_index_new";
    //精选专栏数量
    private String wx_index_topic = "market_wx_index_topic";
    //品牌商显示数量
    private String wx_index_brand = "market_wx_index_brand";
    //分类栏目商品显示数量
    private String wx_catlog_goods = "market_wx_catlog_goods";
    //分类栏目显示数量
    private String wx_catlog_list = "market_wx_catlog_list";
    //人气商品显示数量
    private String wx_index_hot = "market_wx_index_hot";
    HashMap<String, Integer> configMap = new HashMap<>();
    @Autowired
    PosterMapper posterMapper;
    @Autowired
    MarketGoodsMapper goodsMapper;
    @Autowired
    CouponMapper couponMapper;
    @Autowired
    CategoryMapper categoryMapper;
    @Autowired
    BrandMapper brandMapper;
    @Autowired
    TopicMapper topicMapper;
    @Autowired
    ConfigMapper configMapper;
    @Autowired
    CouponUserMapper couponUserMapper;

    public void getSystemConfig() {

        List<GetWxSystemConfig> wxConfig = configMapper.getWxConfig();
        for (GetWxSystemConfig config : wxConfig) {
            if (config.getKeyName().contains("share")) {
                continue;
            }
            configMap.put(config.getKeyName(), Integer.valueOf(config.getKeyValue()));
        }

    }

    @Override
    public HomeIndexVO getHomeIndex() {
        //获取系统配置
        getSystemConfig();


        //记得排除删除的
        List<InnerBanner> innerBanners = getInnerBanners();
        List<InnerNewGoods> innerNewGoods = getInnerNewGoods();
        List<InnerCoupon> innerCoupons = getInnerCoupons();
        List<InnerChannel> innerChannels = getInnerChannels();
        List<InnerBrand> innerBrands = getInnerBrands();
        List<InnerHotGoods> innerHotGoods = getInnerHotGoods();
        List<InnerTopic> innerTopics = getInnerTopicsLimits10();
        List<InnerFloorGoods> innerFloorGoods = getInnerFloorGoods();
        HomeIndexVO homeIndexVO = new HomeIndexVO();
        homeIndexVO.setBanner(innerBanners);
        homeIndexVO.setNewGoodsList(innerNewGoods);
        homeIndexVO.setCouponList(innerCoupons);
        homeIndexVO.setChannel(innerChannels);
        homeIndexVO.setBrandList(innerBrands);
        homeIndexVO.setHotGoodsList(innerHotGoods);
        homeIndexVO.setTopicList(innerTopics);
        homeIndexVO.setFloorGoodsList(innerFloorGoods);

        return homeIndexVO;
    }

    private List<InnerFloorGoods> getInnerFloorGoods() {
        Integer limit1 = configMap.get(wx_catlog_list);
        Integer limit2 = configMap.get(wx_catlog_goods);
        List<Category> categories = categoryMapper.getCategories(limit1);
        ArrayList<InnerFloorGoods> floorGoods = new ArrayList<>();
        for (Category category : categories) {
            Integer id = category.getId();
            List<MarketGoods> goods = goodsMapper.getGoodsByCategoryId(id, limit2);
            floorGoods.add(new InnerFloorGoods(category, goods));
        }

        return floorGoods;

    }

    private List<InnerTopic> getInnerTopicsLimits10() {
        Integer limit = configMap.get(wx_index_topic);
        List<Topic> topicsLimit10 = topicMapper.getTopics(limit);
        List<InnerTopic> innerTopics = new ArrayList<>();
        for (Topic topic : topicsLimit10) {
            innerTopics.add(new InnerTopic(topic));
        }
        return innerTopics;

    }

    private List<InnerHotGoods> getInnerHotGoods() {
        Integer limit = configMap.get(wx_index_hot);
        List<MarketGoods> hotGoodsLimits6 = goodsMapper.getHotGoods(limit);
        ArrayList<InnerHotGoods> innerHotGoods = new ArrayList<>();
        for (MarketGoods marketGoods : hotGoodsLimits6) {

            innerHotGoods.add(new InnerHotGoods(marketGoods));
        }
        return innerHotGoods;


    }

    private List<InnerBrand> getInnerBrands() {
        Integer limit = configMap.get(wx_index_brand);
        List<Brand> brandList = brandMapper.getBrands(limit);
        ArrayList<InnerBrand> innerBrands = new ArrayList<>();
        for (Brand brand : brandList) {
            innerBrands.add(new InnerBrand(brand));
        }
        return innerBrands;


    }

    private List<InnerChannel> getInnerChannels() {
        Integer limit = configMap.get(wx_catlog_list);
        List<Category> categoryList = categoryMapper.getCategories(limit);
        ArrayList<InnerChannel> innerChannels = new ArrayList<>();
        for (Category category : categoryList) {
            innerChannels.add(new InnerChannel(category));
        }
        return innerChannels;
    }

    private List<InnerCoupon> getInnerCoupons() {
        List<Coupon> couponList = couponMapper.getCouponsLimits10();
        List<InnerCoupon> innerCoupons = new ArrayList<>();
        for (Coupon coupon : couponList) {
            if (coupon.getType() != 0) {
                continue;
            }
            //判断是否过期并更新状态
            if (coupon.getEndTime().getTime() <= new Date().getTime()) {
                Integer id = coupon.getId();
                couponMapper.UpdateStatusById(id);
                couponUserMapper.updateStatusByCouponId(id);
                continue;
            }
            innerCoupons.add(new InnerCoupon(coupon));
        }
        return innerCoupons;

    }

    private List<InnerNewGoods> getInnerNewGoods() {
        Integer limit = configMap.get(wx_index_new);
        List<MarketGoods> newGoodsLimits6 = goodsMapper.getNewGoods(limit);
        ArrayList<InnerNewGoods> innerNewGoods = new ArrayList<>();
        for (MarketGoods marketGoods : newGoodsLimits6) {
            innerNewGoods.add(new InnerNewGoods(marketGoods));
        }
        return innerNewGoods;

    }

    private List<InnerBanner> getInnerBanners() {
        List<Poster> posters = posterMapper.getPostersLimit6();
        List<InnerBanner> banners = new ArrayList<>();
        //避免空指针
        if (posters.size() > 0) {
            for (Poster poster : posters) {
                banners.add(new InnerBanner(poster));
            }
        }
        return banners;
    }
}
